package stream;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

/*
收集（collect）
collect，收集，可以说是内容最繁多、功能最丰富的部分了。从字面上去理解，就是把一个流收集起来，最终可以是收集成一个值也可以收集成一个新的集合.
因为流不存储数据，那么在流中的数据完成处理后，需要将流中的数据重新归集到新的集合里。
toList、toSet和toMap比较常用，另外还有toCollection、toConcurrentMap等复杂一些的用法。下面用一个案例演示toList、toSet和toMap。

 */
public class StreamDemo7 {
    //统计(count/averaging)
    public static void main(String[] args) {
        List<Student> list = Arrays.asList(
                new Student("zhangsan", 19, 10000, "武汉"),
                new Student("lisi", 29, 5000, "北京"),
                new Student("wangwu", 12, 9000, "南京"),
                new Student("zhaoliu", 33, 11000, "合肥"),
                new Student("tianqi", 22, 20000, "天津")
        );
        //1.统计员工

        long count = list.stream().count();// count是统计流中元素数量
        System.out.println("员工数量为：" + count);
        System.out.println("--------------------------------");
        //2.方式2
        Long collect = list.stream().collect(Collectors.counting());
        System.out.println("员工数量为：" + collect);

        //3.求所有学生的平均薪资 averagingDouble
        Double collect1 = list.stream().collect(Collectors.averagingDouble(Student::getSalary));
        System.out.println("平均薪资为：" + collect1);

        //4.统计工资 summingInt统计工资
        Integer collect2 = list.stream().collect(Collectors.summingInt(Student::getSalary));
        System.out.println("总薪资为：" + collect2);

        //5.求最高薪资
        Integer integer = list.stream().map(Student::getSalary).collect(Collectors.maxBy(Integer::compare)).get();
        System.out.println("最高薪资为：" + integer);
        //方式2
        Integer integer1 = list.stream().map(Student::getSalary).max(Integer::compareTo).get();
        System.out.println("最高薪资为：" + integer1);

        //6.统计所有信息 信息可以为任意类型
        Integer collect3 = list.stream().collect(Collectors.summingInt(Student::getSalary));
        System.out.println("员工信息为：" + collect3);
        list.stream().collect(Collectors.summarizingInt(Student::getSalary));
        System.out.println("员工信息为：" + collect3);
    }
    //收集 collect
    public static void main1(String[] args) {
        List<Student> list = Arrays.asList(
                new Student("zhangsan", 19, 10000, "武汉"),
                new Student("lisi", 29, 5000, "北京"),
                new Student("wangwu", 12, 9000, "南京"),
                new Student("zhaoliu", 33, 11000, "合肥"),
                new Student("tianqi", 22, 20000, "天津")
        );
        //1.将所有工资大于8000的学生信息收集到一个集合中
        //toList
        List<Student> slist = list.stream().filter(s -> s.getSalary() > 8000).collect(Collectors.toList());
        list.forEach(System.out::println);
        //toSet
        System.out.println("--------------------------------");
        Set<Student> collect = list.stream().filter(s -> s.getSalary() > 8000).collect(Collectors.toSet());
        collect.forEach(System.out::println);
        System.out.println("--------------------------------");
        //toMap
        Map<String, Integer> collect2 = list.stream().filter(s -> s.getSalary() > 8000).collect(Collectors.toMap(Student::getName, Student::getSalary));
        collect2.forEach((k, v) -> System.out.println(k + "=" + v));
    }
}
